package com.yhd.controller;

import com.yhd.entity.dto.CourseAboutDto;
import com.yhd.entity.dto.CourseBookedDto;
import com.yhd.entity.dto.CourseDto;
import com.yhd.entity.dto.CourseScheduleDetailDto;
import com.yhd.entity.query.CourseAboutVO;
import com.yhd.entity.query.CourseBookedVO;
import com.yhd.entity.query.CourseScheduleDetailVO;
import com.yhd.entity.query.CourseVO;
import com.yhd.entity.result.ResultVo;
import com.yhd.service.CourseBookedService;
import com.yhd.service.CourseService;
import com.yhd.service.CycleImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(value = "小程序课程相关")
@Slf4j
@RequestMapping(value = "/app/course")
@CrossOrigin
public class AppCourseController extends WebBaseController {

    @Autowired
    private CycleImgService cycleImgService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseBookedService courseBookedService;
    
    
    @ApiOperation(value = "课程详情", httpMethod = "GET",notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "courseId", value = "课程id", required = true, paramType = "int")})
    @GetMapping("/detail")
    @ResponseBody
    public ResultVo<CourseDto> courseDetail(@RequestParam Integer courseId) {
        CourseDto result = courseService.courseDetail(courseId, getUserInfo(request));
        return ResultVo.success(result);
    }
    
    @ApiOperation(value = "下单（体验预约/购买课程）", httpMethod = "POST", notes = "{\n" +
            "  \"courseId\": \"课程id\"\",\n" +
            "  \"subType\": \"类型：1=预约体验，0=直接购买\"\",\n" +
            "  \"isAgreeAgreement\": \"是否同意课程协议 1是 0否\"\",\n" +
            "  \"sex\": \"性别 0女 1男\"\",\n" +
            "  \"userName\": \"用户名\"\",\n" +
            "  \"cardType\": \"证件类型\"\",\n" +
            "  \"cardNo\": \"证件号码\"\",\n" +
            "  \"birthday\": \"生日\"\",\n" +
            "  \"phone\": \"电话\"\",\n" +
            "  \"company\": \"公司\"\",\n" +
            "  \"position\": \"职位\"\",\n" +
            "  \"position\": \"职位\"\",\n" +
            "  \"tuser\": \"招生老师\"\",\n" +
            "  \"referrerUser\": \"推荐人\"\n" +
            "}")
    @PostMapping("/order")
    @ResponseBody
    public ResultVo<CourseBookedDto> order(@RequestBody CourseBookedVO bookedVO) throws Exception {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        bookedVO.setType("course");
        return courseService.order(bookedVO, getUserInfo(request));
       
    }

    @ApiOperation(value = "课程收藏/取消收藏", httpMethod = "POST", notes = "{\n" +
            "  \"collected\": \"收藏：1=收藏，0=取消收藏\"\",\n" +
            "  \"id\": \"课程id\"\n" +
            "}")
    @PostMapping("/collect")
    @ResponseBody
    public ResultVo<CourseDto> courseCollect(@RequestBody CourseVO courseVO) throws Exception {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        CourseDto courseDto = courseService.collect(courseVO, getUserInfo(request));
        return ResultVo.success(courseDto);
    }

    @ApiOperation(value = "约课", httpMethod = "POST", notes = "{\n" +
            "  \"courseSchId\": \"排课id\"\",\n" +
            "  \"orderId\": \"订单表id\"\n" +
            "}")
    @PostMapping("/about")
    @ResponseBody
    public ResultVo<CourseAboutDto> about(@RequestBody CourseAboutVO aboutVO) throws Exception {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        ResultVo<CourseAboutDto> resultVo = courseService.about(aboutVO, getUserInfo(request));
        return resultVo;
    }

    


    @ApiOperation(value = "取消订单", httpMethod = "POST", notes = "{\n" +
            "  \"id\": \"订单表id\"\n" +
            "}")
    @PostMapping("/cancelOrder")
    @ResponseBody
    public ResultVo<CourseBookedDto> cancelOrder(@RequestBody CourseBookedVO bookedVO) throws Exception {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        CourseBookedDto bookedDto = courseBookedService.cancelOrder(bookedVO, getUserInfo(request));
        return ResultVo.success(bookedDto);
    }
    
    @ApiOperation(value = "课程排课列表V1", httpMethod = "GET",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程id", required = true, paramType = "int"),
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false)})
    @GetMapping("/courseSchedule")
    @ResponseBody
    public ResultVo<List<CourseScheduleDetailDto>> courseSchedule(CourseScheduleDetailVO courseScheduleDetailVO) {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        List<CourseScheduleDetailDto> scheduleDetailDtos = courseService.courseSchedule(courseScheduleDetailVO, getUserInfo(request));
        return ResultVo.success(scheduleDetailDtos);
    }

    @ApiOperation(value = "课程排课列表V2（多对多关系，暂时弃用）", httpMethod = "GET",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderIds", value = "多个订单ID字符串，逗号分隔", required = true, paramType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false)})
    @GetMapping("/courseScheduleList")
    @ResponseBody
    public ResultVo<List<CourseScheduleDetailDto>> courseScheduleList(CourseScheduleDetailVO courseScheduleDetailVO) {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        List<CourseScheduleDetailDto> scheduleDetailDtos = courseService.courseScheduleList(courseScheduleDetailVO, getUserInfo(request));
        return ResultVo.success(scheduleDetailDtos);
    }
    
    
    @ApiOperation(value = "订单详情", httpMethod = "GET",notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "课程id", required = true, paramType = "int")})
    @GetMapping("/orderDetail")
    @ResponseBody
    public ResultVo<CourseBookedDto> orderDetail(Integer orderId) {
        if (getUserInfo(request) == null) {
            return ResultVo.failure("10001","无访问权限，请先登录！");
        }
        CourseBookedDto result = courseService.orderDetail(orderId, getUserInfo(request));
        return ResultVo.success(result);
    }
}
